/**
  ******************************************************************************
  * @file    main.c
  * @author  AE Team
  * @version 1.0.3
  * @date    2024-05-28
  * @brief   本样例演示通过SPI驱动OLED，实现OLED显示，OLED驱动芯片为SSD1306
  *          本案例选择的OLED是中景园电子0.96 寸OLED(128x64) 显示屏
  *          店铺地址：http://shop73023976.taobao.com/?spm=2013.1.0.0.M4PqC2
  * @note    硬件平台：SYM32L010_StartKit_V1.0
  *          接线说明：断开J17-J21, J23, J28
  *                    OLED液晶与StartKit的接线详见oled.h
  *
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2024 SIYIMicro.
  * All rights reserved.</center></h2>
  *
  *
  ******************************************************************************
  */


/******************************************************************************/
/* Include files                                                              */
/******************************************************************************/
#include "main.h"
#include "sym32l010_hal_sysctrl.h"
#include "sym32l010_hal_systick.h"
#include "sym32l010_hal_gpio.h"
#include "sym32l010_hal_spi.h"
#include "oled.h"
#include "bmp.h"


/******************************************************************************/
/* Local pre-processor symbols/macros ('#define')                             */
/******************************************************************************/

/******************************************************************************/
/* Global variable definitions (declared in header file with 'extern')        */
/******************************************************************************/
SPI_HandleTypeDef hspi1;

/******************************************************************************/
/* Local type definitions ('typedef')                                         */
/******************************************************************************/

/******************************************************************************/
/* Local function prototypes ('static')                                       */
/******************************************************************************/
static void OLED_Port_Init(void);

/******************************************************************************/
/* Local variable definitions ('static')                                      */
/******************************************************************************/

/******************************************************************************/
/* Local pre-processor symbols/macros ('#define')                             */
/******************************************************************************/

/******************************************************************************/
/* Function implementation - global ('extern') and local ('static')           */
/******************************************************************************/

/*******************************************************************************
 ** \brief  Main function of project
 **
 ** \return uint32_t return value, if needed
 **
 **
 ******************************************************************************/
int32_t main(void)
{
    uint8_t t = 0;
    uint8_t ascii = 0;

    //------------------------------------------------------------
    // 配置HCLK = 48MHz
    HAL_SysCtrl_ConfigWaitCycle(48000000);
    HAL_SysCtrl_HSI_SetDiv(HSIOSC_TO_HSI48MHZ);

    // 初始化SysTick
    HAL_InitTick(48000000);

    OLED_Port_Init();
    OLED_Init();
    OLED_Clear();

    while (1)
    {
        OLED_Clear();

        OLED_ShowCHinese(18, 0, 0);                                   // 矽
        OLED_ShowCHinese(36, 0, 1);                                   // 翊
        OLED_ShowCHinese(54, 0, 2);                                   // 微
        OLED_ShowCHinese(72, 0, 3);                                   // 电
        OLED_ShowCHinese(90, 0, 4);                                   // 子
        OLED_ShowString(0, 3, "0.96' OLED TEST");
        OLED_ShowString(0, 6, "SiYiMicro");

        ascii = (t % 95) + 32;
        OLED_ShowChar(87, 6, ascii);                                  // 显示ASCII字符
        OLED_ShowNum(103, 6, ascii, 3, 16);                           // 显示ASCII字符的十进制值
        t++;
        HAL_Delay(2000);

        OLED_DrawBMP(0, 0, 128, 8, BMP1);                             // 图片显示(图片显示慎用，生成的字表较大，会占用较多空间，FLASH空间8K以下慎用)
        HAL_Delay(2000);
        OLED_DrawBMP(0, 0, 128, 8, BMP2);
        HAL_Delay(2000);
    }
}


/*******************************************************************************
 * @brief  OLED 端口初始化
 * @param  None
 * @retval None
 */
static void OLED_Port_Init(void)
{
    //------------------------------------------------------------
    // 配置OLED端口的所有GPIO
    GPIO_InitTypeDef GPIO_InitStruct;
    MemClr(&GPIO_InitStruct, sizeof(GPIO_InitStruct));

    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

    OLED_RES_PORT_CLK_EN();
    GPIO_InitStruct.Pins = OLED_RES_PIN;
    HAL_GPIO_Init(OLED_RES_PORT, &GPIO_InitStruct);

    OLED_DC_PORT_CLK_EN();
    GPIO_InitStruct.Pins = OLED_DC_PIN;
    HAL_GPIO_Init(OLED_DC_PORT, &GPIO_InitStruct);

    OLED_CS_PORT_CLK_EN();
    OLED_CS_AF();
    GPIO_InitStruct.Pins = OLED_CS_PIN;
    HAL_GPIO_Init(OLED_CS_PORT, &GPIO_InitStruct);

    OLED_D0_PORT_CLK_EN();
    OLED_D0_AF();
    GPIO_InitStruct.Pins = OLED_D0_PIN;
    HAL_GPIO_Init(OLED_D0_PORT, &GPIO_InitStruct);

    OLED_D1_PORT_CLK_EN();
    OLED_D1_AF();
    GPIO_InitStruct.Pins = OLED_D1_PIN;
    HAL_GPIO_Init(OLED_D1_PORT, &GPIO_InitStruct);

    //------------------------------------------------------------
    // 配置OLED接口的SPI
    MemClr(&hspi1, sizeof(hspi1));

    OLED_CHIP_SPI_CLK_EN();
    hspi1.Instance         = OLED_CHIP_SPI;
    hspi1.Init.WorkMode    = SPI_WORKMODE_MASTER;
    hspi1.Init.CommLines   = SPI_COMM2LINES_FullDuplex;
    hspi1.Init.DataWidth   = SPI_DATAWIDTH_8BIT;
    hspi1.Init.SckPolarity = SPI_SCKPOLARITY_NEGATIVE;
    hspi1.Init.SckPhase    = SPI_SCKPHASE_1EDGE;
    hspi1.Init.SckSource   = SPI_SCKSRC_PCLKDIV16;                    // SckFreq = 48MHz/16= 3MHz
    hspi1.Init.FirstBit    = SPI_FIRSTBIT_MSB;
    hspi1.Init.NcsMode     = SPI_NCS_HARD_OUTPUT;
    HAL_SPI_Init(&hspi1);
    HAL_SPI_Enable(&hspi1);
}


/*******************************************************************************
 ** EOF (not truncated)
 ******************************************************************************/
#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
    /* USER CODE BEGIN */
    /* User can add his own implementation to report the file name and line number,
       tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
    /* USER CODE END */
}
#endif /* USE_FULL_ASSERT */


/************************ (C) COPYRIGHT SIYIMicro *****END OF FILE*************/
